package com.example.widgettest_206shj;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener {

    private TextView tv;
    private EditText et;
    private Button btngettext, btnsetimg;
    private ImageButton ibtn;
    private ImageView iv;

    private RadioButton rbnan, rbnv;
    private RadioGroup rg;
    private CheckBox cblan, cbzu, cbyou;
    private Button btngetgender, btngetfun;

    private View bg;

    private String strfun = "";//保存兴趣字符串

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId){
            case R.id.radioButton1:
                tv.setText("您选择的性别为“男”");
                break;
            case R.id.radioButton2:
                tv.setText("您选择的性别为“女”");
                break;
        }
    }

    public void init(){
        //获取textView、editText、Button（获取并设置文本、被设置了背景图片的按钮）、imageButton、imageView控件
        tv = (TextView) findViewById(R.id.TextView);
        et = (EditText) findViewById(R.id.editText);
        btngettext = (Button) findViewById(R.id.button);
        btnsetimg = (Button) findViewById(R.id.button2);
        ibtn = (ImageButton) findViewById(R.id.imageButton);
        iv = (ImageView) findViewById(R.id.imageView);

        //添加textView、imageView、button监听器
        btngettext.setOnClickListener(this);
        btnsetimg.setOnClickListener(this);
        ibtn.setOnClickListener(this);

        //获取radioButton、radiogroup、checkBox、Button控件
        rbnan = (RadioButton) findViewById(R.id.radioButton1);
        rbnv = (RadioButton) findViewById(R.id.radioButton2);
        rg = (RadioGroup) findViewById(R.id.radioGroup);
        cblan = (CheckBox) findViewById(R.id.checkBox);
        cbzu = (CheckBox) findViewById(R.id.checkBox2);
        cbyou = (CheckBox) findViewById(R.id.checkBox3);
        btngetgender = (Button) findViewById(R.id.button4);
        btngetfun = (Button) findViewById(R.id.button5);

        //按钮监听器
        btngetgender.setOnClickListener(this);
        btngetfun.setOnClickListener(this);

        //radioGroup监听器
        rg.setOnCheckedChangeListener(this);

        //checkBox监听器
        //  篮球
        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)//checkBox被勾选
                    strfun += "篮球 ";
                else
                    strfun = strfun.replace("篮球 " , "");
                tv.setText(strfun);
            }
        });
        //  足球
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)//checkBox被勾选
                    strfun += "足球 ";
                else
                    strfun = strfun.replace("足球 " , "");
                tv.setText(strfun);
            }
        });
        //  游泳
        cbyou.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)//checkBox被勾选
                    strfun += "游泳 ";
                else
                    strfun = strfun.replace("游泳 " , "");
                tv.setText(strfun);
            }
        });

        //获取背景
        bg = findViewById(R.id.bg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button://设置文本
                String str = et.getText().toString();
                tv.setText(str);
                break;
            case R.id.button2://设置图片
                iv.setImageResource(R.drawable.diablo);
                bg.setBackgroundResource(R.drawable.diablo);
                break;
            case R.id.imageButton:
                Intent intent = new Intent(MainActivity.this, NewActivity.class);
                intent.putExtra("param1","hello,  ");
                intent.putExtra("param2",5);
                startActivity(intent);
                break;
            case R.id.button4:
                if(rbnan.isChecked())
                    tv.setText("您选择的性别为“男”");
                if(rbnv.isChecked())
                    tv.setText("您选择的性别为“女”");
                break;
            case R.id.button5:
                String msg = "";
                if(cblan.isChecked())
                    msg += "篮球 ";
                if(cbzu.isChecked())
                    msg += "足球 ";
                if(cbyou.isChecked())
                    msg += "游泳";
                tv.setText(msg);
                break;
        }
    }
    
}
